﻿//======================================================================
//
//	Cross-Platform XNA Menu System
//  http://cpxms.codeplex.com/
//
//  All code and content licensed under Microsoft Public License (Ms-PL)	
//
//======================================================================
//
//	File:		EditorSupport.cs
//
//	Author:		MJP
//
//	Date:		04/17/09
//
//======================================================================

// This class contains "dummy" definitions of classes used for editor 
// support, but aren't available  in the Xbox 360 version of the 
// project.  This is so the code doesn't have to be littered with
// conditional compilation statements.


using System;
using System.Reflection;

using Microsoft.Xna.Framework.Content;

namespace CPXMenu
{
#if WINDOWS
/// <summary>
    /// Used to allow the user to browse for content in the PropertyGrid
    /// </summary>
    /// <typeparam name="T">The Type of content to display in the ContentBrowser</typeparam>
    public class ContentPickerEditor<T> : System.Drawing.Design.UITypeEditor
    {
        /// <summary>
        /// Gets the edit style for the editor
        /// </summary>
        /// <param name="context">Editor context</param>
        /// <returns>The editor style, always returns Modal</returns>
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return System.Drawing.Design.UITypeEditorEditStyle.Modal;
        }

        /// <summary>
        /// Allows the user to edit the value by bringing up the 
        /// ContentBrowserDialog dialog.
        /// </summary>
        /// <param name="context">The editor context</param>
        /// <param name="provider">Service provider</param>
        /// <param name="value">The current value of the property</param>
        /// <returns>The new value of the property</returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,
                                            IServiceProvider provider,
                                            object value)
        {
            System.Windows.Forms.Design.IWindowsFormsEditorService editorService = null;

            if (provider != null)
            {
                editorService = provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService)) as System.Windows.Forms.Design.IWindowsFormsEditorService;
            }

            if (editorService != null)
            {
                // Pop up our dialog
                // NOTE: We're doing this through nasty reflection so that we can keep all
                // of the content browser and importing stuff in the Editor's assembly
                IContentOwner item = context.Instance as IContentOwner;
                Assembly assembly = Assembly.LoadFile(System.Windows.Forms.Application.ExecutablePath);
                object[] parameters = new object[5];
                parameters[0] = value;
                parameters[1] = typeof(T);
                parameters[2] = item.Content.ServiceProvider;
                parameters[3] = item.Content.RootDirectory;
                parameters[4] = item;

                Type browserType = assembly.GetType("CPXMenuEditor.Forms.ContentPicker");
                System.Windows.Forms.Form picker = (System.Windows.Forms.Form)Activator.CreateInstance(browserType, parameters);
                if (editorService.ShowDialog(picker) == System.Windows.Forms.DialogResult.OK)
                {
                    PropertyInfo property = browserType.GetProperty("SelectedContentFile");
                    value = property.GetValue(picker, null);
                }
            }

            return value;
        }
    }    
#endif

    public interface IContentOwner
    {
        ContentManager Content { get; }
        MenuItem OwnerItem { get; }
    }

#if XBOX
    public class BrowsableAttribute : Attribute
    {
        public BrowsableAttribute(bool browsable)
        {
        }
    }

    public class DescriptionAttribute : Attribute
    {
        string description = string.Empty;

        public DescriptionAttribute(String description)
        {
            this.description = description;   
        }

        public string Description
        {
            get { return description; }
        }
    }

    public class CategoryAttribute : Attribute
    {
        public CategoryAttribute(String category)
        {
        }
    }

    public class EditorAttribute : Attribute
    {
        public EditorAttribute(Type type1, Type type2)
        {
        }
    }

    public class ReadOnlyAttribute : Attribute
    {
        public ReadOnlyAttribute(bool readOnly)
        {
        }
    }

    public class TypeConverterAttribute : Attribute
    {
        public TypeConverterAttribute(Type type)
        {
        }
    }

    public class DisplayNameAttribute : Attribute
    {
        string displayName = string.Empty;

        public DisplayNameAttribute(string displayName)
        {
            this.displayName = displayName;
        }

        public string DisplayName
        {
            get { return displayName; }
        }
    }

    public class ExpandableObjectConverter
    {
    }    

    public class UITypeEditor
    {
    }

    public class PagePickerEditor : UITypeEditor
    {
    }

    public class ColorPickerEditor : UITypeEditor
    {
    }

    public class FlipEffectConfigEditor : UITypeEditor
    {
    }
    
#endif

}

#if XBOX
namespace System.Drawing.Design
{
    /// <summary>
	/// Just used to actually put something in the namespace for the PC.
	/// If we didn't have this, while you're typing code VS will have a 
	/// red squiggly by the namespace because it doesn't exist.
	/// </summary>
    internal class DummyClass
    {
    }

    internal class ContentBrowserEditor<T>
    {

    }
}
#endif